package com.cpp.server.tools;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cpp.common.constant.redis.RedisConstant;
import com.cpp.pojo.dto.aiTool.StudentPracticeDTO;
import com.cpp.pojo.pojo.*;
import com.cpp.server.context.UserHolder;
import com.cpp.server.mapper.CourseMapper;
import com.cpp.server.mapper.KnowledgeMapper;
import com.cpp.server.mapper.AnswerRecordMapper;
import com.cpp.server.mapper.QuestionMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor
@Slf4j
public class StudentPracticeTool {
    private final CourseMapper courseMapper;
    private final AnswerRecordMapper answerRecordMapper;
    private final KnowledgeMapper knowledgeMapper;
    private final QuestionMapper questionMapper;
    private final StringRedisTemplate stringRedisTemplate;

    @Tool(description = "获取知识点信息")
    public List<Knowledge> getKnowledge(
            @ToolParam(description = "课程id")Long courseId
    ) {
        LambdaQueryWrapper<Knowledge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Knowledge::getCourseId ,courseId);
        List<Knowledge> knowledges = knowledgeMapper.selectList(queryWrapper);
        log.info("调用工具获取知识点信息条件curseId:{}，结果:{}", courseId,knowledges);
        return knowledges;
    }
    @Tool(description = "把生成的题目保存到数据库")
    public void insertQuestion(
            @ToolParam(description = "题目列表") List<StudentPracticeDTO> studentPracticeDTOS
    ){
        User currentUser = UserHolder.getCurrentUser();
        log.info("调用工具保存题目:{}", studentPracticeDTOS);
        List<Question> questions =  BeanUtil.copyToList(studentPracticeDTOS, Question.class);
        stringRedisTemplate.opsForValue().set(RedisConstant.STUDENT_PRACTICE_KEY+currentUser.getId(), JSON.toJSONString(questions),  60, TimeUnit.MINUTES);

    }

}
